package com.udaos.modules.trainer.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.adaptor.WhaleAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.project.Ud_train_project;
import com.udaos.models.school.Ud_school;
import com.udaos.models.trainer.Ud_trainer;
import com.udaos.models.trainer.Ud_trainer_project;
import com.udaos.modules.project.services.UdProjectService;
import com.udaos.modules.school.services.UdSchoolService;
import com.udaos.modules.trainer.services.UdTrainerService;


/**
 * @author mutaotao
 * @time 2016-12-15 10:36:44
 * 
 */
@IocBean
@At("/platform/trainer")
@Filters({ @By(type = PrivateFilter.class)})
public class UdTrainerController {
	@Inject
	private UdTrainerService udTrainerService;
	
	@Inject
	private UdSchoolService schoolService;
	
	@Inject
	private UdProjectService projectService;
	
	

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/trainer/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("mobile") String mobile,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		if(!Strings.isBlank(name))
			cnd.and("name", "=", name);
		if(!Strings.isBlank(mobile))
			cnd.and("mobile", "=", mobile);
		return udTrainerService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 增加
	 */
	@At
	@Ok("beetl:/platform/trainer/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Ud_school> schoolList= schoolService.find();
		List<Ud_train_project> projects=projectService.find();
		map.put("schoolList", schoolList);
		map.put("projects", projects);
		return map;
	}
	
	/**
	 * do新增
	 * @param ud_coach
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object addDo(@Param("..") Ud_trainer ud_trainer, String projects,HttpServletRequest req) {
		try {
			udTrainerService.save(ud_trainer,projects);
			return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error("保存失败");
		}
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/trainer/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Ud_school> schoolList= schoolService.find();
		List<Ud_train_project> projects=projectService.find();
		List<Ud_trainer_project> trainerProjects=udTrainerService.findTrainerProjects(id);
		map.put("schoolList", schoolList);
		map.put("trainerProjects", trainerProjects);
		map.put("projects", projects);
		map.put("trainer", udTrainerService.fetch(id));
		return map;
	}
	
	/**
	 * do修改
	 * @param ud_coach
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@AdaptBy(type = WhaleAdaptor.class)
	public Object editDo(@Param("..") Ud_trainer ud_trainer,String projects,HttpServletRequest req) {
		try {
			udTrainerService.updateTrainer(ud_trainer,projects);
			return Result.success("修改成功");
		} catch (Exception e) {
			return Result.error("修改失败");
		}
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@At("/delete/?")
	@Ok("json")
	@RequiresAuthentication
	public Object delete(String id){
		try {
			Ud_trainer trainer=udTrainerService.fetch(id);
			trainer.setDelFlag(true);
			udTrainerService.updateIgnoreNull(trainer);
			return Result.success("删除成功");
		} catch (Exception e) {
			return Result.error("删除失败");
		}
	}
	
	/**
	 * 获取驾校名称
	 * @param id
	 * @return
	 */
	@At
	@Ok("json") 
	@RequiresAuthentication
	public Object getSchoolName(String id){
		if(Strings.isBlank(id)){
			return "";
		}else{
			Ud_school school=schoolService.fetch(id);
			if(null != school){
				return school.getName();
			}
			return "";
		}
	}
}